﻿using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Interactivity;
using System.Windows.Media;

namespace Chambills.PopUpBehavior
{
    public class PopUpBehaviour : Behavior<Grid>
    {

        public static readonly DependencyProperty VisibleProperty =
            DependencyProperty.Register("Visible", typeof(bool), typeof(PopUpBehaviour), new PropertyMetadata(OnVisibleChanged));


        public static readonly DependencyProperty ModalProperty =
            DependencyProperty.Register("Modal", typeof(bool), typeof(PopUpBehaviour));
        

        public static readonly DependencyProperty PopUpProperty =
            DependencyProperty.Register("PopUp", typeof(UIElement), typeof(PopUpBehaviour));

        private readonly Grid grid;

        public PopUpBehaviour()
        {

            grid = new Grid { Name = "PopUpBehaviour_DrumSkin" };
            grid.CommandBindings.Add(new CommandBinding(ApplicationCommands.Close, (sender, e) => SetCurrentValue(VisibleProperty, false)));
        }

        public UIElement PopUp
        {
            get { return (UIElement)GetValue(PopUpProperty); }
            set { SetValue(PopUpProperty, value); }
        }

        public bool Modal
        {
            get { return (bool)GetValue(ModalProperty); }
            set { SetValue(ModalProperty, value); }
        }

        public DisableBackGroundBehaviour ModalBehaviour
        {
            get; set;
        }

        public bool Visible
        {
            get { return (bool)GetValue(VisibleProperty); }
            set { SetValue(VisibleProperty, value); }
        }

        private static void OnVisibleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var popup = d as PopUpBehaviour;
            if (popup == null || popup.AssociatedObject == null)
                return;

            if (e.NewValue == e.OldValue)
                return;

            if (Convert.ToBoolean(e.NewValue))
                popup.Show();
            else
                popup.Hide();
        }

        private void Hide()
        {
            if (grid != null)
            {
                AssociatedObject.Children.Remove(grid);
                if (PopUp != null)
                    grid.Children.Remove(PopUp);
                if (ModalBehaviour != null)
                    Interaction.GetBehaviors(grid).Remove(ModalBehaviour);
            }

        }

        private void Show()
        {
            grid.SetValue(Panel.ZIndexProperty, 998);
            grid.SetValue(Grid.RowSpanProperty, Math.Max(1, AssociatedObject.RowDefinitions.Count));
            grid.SetValue(Grid.ColumnSpanProperty, Math.Max(1, AssociatedObject.ColumnDefinitions.Count));


            if (Modal)
            {
                Interaction.GetBehaviors(grid).Add(ModalBehaviour ?? new DisableBackGroundBehaviour { Background = Brushes.Gray});
            }

            PopUp.SetValue(Panel.ZIndexProperty, 1000);

            grid.Children.Add(PopUp);

            AssociatedObject.Children.Add(grid);
            PopUp.Focusable = true;
            PopUp.Focus();
        }

    }
}